package com.hyg.lib_music.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hyg.lib_music.R;
import com.hyg.lib_music.modle.NewMusicCategoryData;
import com.hyg.lib_music.ui.activity.CategorySecondActivity;

import java.util.ArrayList;

/**
 * @author: Administrator
 * @date: 2021/4/7
 */
public class MusicCategotyAdapter extends RecyclerView.Adapter<MusicCategotyAdapter.ViewHolde> {

    Context context;
    ArrayList<NewMusicCategoryData.DataDTO> list;

    public MusicCategotyAdapter(Context context, ArrayList<NewMusicCategoryData.DataDTO> list) {
        this.context = context;
        this.list = list;
    }


    @NonNull
    @Override
    public ViewHolde onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_category_list, parent, false);
        return new ViewHolde(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolde holder, int position) {
        Glide.with(context).load(list.get(position).logoUrl).into(holder.imageView);
        holder.textView.setText(list.get(position).cateName);

        int width=((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
        ViewGroup.LayoutParams params=holder.imageView.getLayoutParams();
        params.width= (int) (width-3*(context.getResources().getDimension(R.dimen.minimum3)))/2;
        holder.imageView.setLayoutParams(params);

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, CategorySecondActivity.class);
                intent.putExtra("data", list.get(position));
                context.startActivity( intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public class ViewHolde extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;

        public ViewHolde(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_image);
            textView = itemView.findViewById(R.id.tv_title);
        }
    }


}
